package net.xumf.support.auth;

import java.util.List;

public abstract class VerificationAbstract implements Verification {

    @Override
    public boolean hasRole(String role) {
        if (null == getRoles()) return false;
        return getRoles().contains(role);
    }

    @Override
    public boolean hasRoleAnd(List<String> roles) {
        if (null == getRoles()) return false;
        for (String role : roles) {
            if (!getRoles().contains(role)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean hasRoleOr(List<String> roles) {
        if (null == getRoles()) return false;
        for (String role : roles) {
            if (getRoles().contains(role)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean hasPermission(String permission) {
        if (null == getPermissions()) return false;
        return getPermissions().contains(permission);
    }

    @Override
    public boolean hasPermissionsAnd(List<String> permissions) {
        if (null == getPermissions()) return false;
        for (String permission : permissions) {
            if (!getPermissions().contains(permission)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean hasPermissionsOr(List<String> permissions) {
        if (null == getPermissions()) return false;
        for (String permission : permissions) {
            if (getPermissions().contains(permission)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String getRoleKey() {
        return ROLE_KEY + CurrentUser.instance().get().getLoginName();
    }

    @Override
    public String getPermissionKey() {
        return PERMISSION_KEY + CurrentUser.instance().get().getLoginName();
    }
}
